package com.jiazhong.servlet.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("*.html")
public class IsLoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("开始教研用户是否登陆");
        String uri = request.getRequestURI();
        System.out.println("用户访问的路径是：" + uri);
        if (uri.contains("login.html")) {
            chain.doFilter(request, response);
            return;
        }
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        System.out.println("用户是否登陆了：" + user);
        if (user != null) {
            chain.doFilter(request, response);
            return;
        }
        response.sendRedirect("login.html");
    }
}
